package com.bank.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.code.RespCode;
import com.bank.dao.TimeAccountMapper;
import com.bank.entity.DemandAccount;
import com.bank.entity.Subject;
import com.bank.entity.TimeAccount;
import com.bank.exception.CommonException;
import com.bank.service.TimeAccountService;
import com.bank.util.CommonUtil;

@Service(value="timeAccountService")
public class TimeAccountServiceImpl implements TimeAccountService {

	
	@Autowired
	TimeAccountMapper   timeAccountMapper;

	public void openAccount(Map<String, Object> params) throws CommonException {
		TimeAccount timeAccount = null;
		Subject subject = CommonUtil.getSubjectInfo(CommonUtil.DEMAND_ACCT_SUBJECT);
		if(subject == null){
			throw new CommonException(RespCode.DATA_IS_NULL, "科目号未配置");
		}
		String  number = (String)params.get("accounting_ori")+(String)params.get("senior_oir")+subject.getSubject_number()+(String)params.get("currency")+"02";
		timeAccount = timeAccountMapper.selectByPrimaryKey(number);
		if(timeAccount!= null){
			throw new CommonException(RespCode.ACCT_EXISTS, "账号已存在");
		}else
		{
			timeAccount = new TimeAccount();
		}
		
		timeAccount.setAccount_status("0");
		timeAccount.setNumber(number);//账号
		timeAccount.setAccounting_ori((String)params.get("accounting_ori"));
		timeAccount.setSenior_oir((String)params.get("senior_oir"));
		timeAccount.setAccount_subject(CommonUtil.DEMAND_ACCT_SUBJECT);
		timeAccount.setBalance_direct(subject.getSubject_balance_direct());
		timeAccount.setCurrency((String)params.get("currency"));
		timeAccount.setBalance(0.00);
		timeAccount.setBalance_pre(0.00);
		timeAccount.setCustomer_number((String)params.get("customer_number"));
		timeAccount.setCustomer_name((String)params.get("customer_name"));
		timeAccount.setLast_trade_date("0");
		timeAccount.setProduct_number(0.00);
		timeAccount.setRate_id((String)params.get("rate_value"));
		timeAccount.setPassword(CommonUtil.passwordEncryption((String)params.get("password")));
		timeAccount.setAccount_status("0");
		timeAccount.setProduct_type((String)params.get("deposit_time")); //存期
		timeAccountMapper.insertSelective(timeAccount);

	}

}
